
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.手动实现broadcast和dispatch方法 · Gary的小窝</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gary">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../js/form.html" />
    
    
    <link rel="prev" href="1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    写在前面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../leetcode/">
            
                <a href="../leetcode/">
            
                    
                    Leetcode之旅
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../leetcode/1.html">
            
                <a href="../leetcode/1.html">
            
                    
                    1.两数之和
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../leetcode/2.html">
            
                <a href="../leetcode/2.html">
            
                    
                    2.两数相加
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../leetcode/3.html">
            
                <a href="../leetcode/3.html">
            
                    
                    3.无重复字符的最长子串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../leetcode/4.html">
            
                <a href="../leetcode/4.html">
            
                    
                    4.寻找两个有序数组的中位数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../leetcode/6.html">
            
                <a href="../leetcode/6.html">
            
                    
                    6.Z字形变换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../leetcode/9.html">
            
                <a href="../leetcode/9.html">
            
                    
                    9.回文数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../leetcode/11.html">
            
                <a href="../leetcode/11.html">
            
                    
                    11.盛最多水的容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../leetcode/12.html">
            
                <a href="../leetcode/12.html">
            
                    
                    12.整数转罗马数字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../leetcode/13.html">
            
                <a href="../leetcode/13.html">
            
                    
                    13.罗马数字转整数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../leetcode/14.html">
            
                <a href="../leetcode/14.html">
            
                    
                    14.最长公共前缀
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../leetcode/322.html">
            
                <a href="../leetcode/322.html">
            
                    
                    322.零钱兑换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../leetcode/72.html">
            
                <a href="../leetcode/72.html">
            
                    
                    72.编辑距离
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../flutter/">
            
                <a href="../flutter/">
            
                    
                    Flutter从入门到放弃
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../flutter/dart1.html">
            
                <a href="../flutter/dart1.html">
            
                    
                    1.Dart语言(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../flutter/dart2.html">
            
                <a href="../flutter/dart2.html">
            
                    
                    2.Dart语言(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../flutter/widget1.html">
            
                <a href="../flutter/widget1.html">
            
                    
                    3.Flutter初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../flutter/containerText.html">
            
                <a href="../flutter/containerText.html">
            
                    
                    4.Container和Text组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../flutter/image.html">
            
                <a href="../flutter/image.html">
            
                    
                    5.Image组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../flutter/chapter2.html">
            
                <a href="../flutter/chapter2.html">
            
                    
                    6.Flutter 技术总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    Vue组件精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="1.html">
            
                <a href="1.html">
            
                    
                    1.provide/inject
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="2.html">
            
                <a href="2.html">
            
                    
                    2.手动实现broadcast和dispatch方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../js/form.html">
            
                <a href="../js/form.html">
            
                    
                    3.自己动手实现带校验的Form表单组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../js/">
            
                <a href="../js/">
            
                    
                    JavaScript进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../js/proxy.html">
            
                <a href="../js/proxy.html">
            
                    
                    1.Vue.js 3.0中的响应式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../js/el.html">
            
                <a href="../js/el.html">
            
                    
                    2.Element UI 源码初探
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../js/bind.html">
            
                <a href="../js/bind.html">
            
                    
                    3.bind的模拟实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../js/proto.html">
            
                <a href="../js/proto.html">
            
                    
                    4.JS原型和原型链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../js/call.html">
            
                <a href="../js/call.html">
            
                    
                    5.模拟实现call和apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../js/ui.html">
            
                <a href="../js/ui.html">
            
                    
                    6.使用Vue cli3搭建组件库并发布到 npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../js/microFE.html">
            
                <a href="../js/microFE.html">
            
                    
                    7.记一次基于qiankun的微前端的改造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../js/git.html">
            
                <a href="../js/git.html">
            
                    
                    8.Git的使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vueLibrary/">
            
                <a href="../vueLibrary/">
            
                    
                    基于Vue的PC端的组件库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vueLibrary/1.html">
            
                <a href="../vueLibrary/1.html">
            
                    
                    1.环境搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../life/">
            
                <a href="../life/">
            
                    
                    网事杂谈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../life/1.html">
            
                <a href="../life/1.html">
            
                    
                    1.周杰伦专辑赏析--叶惠美
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../life/2.html">
            
                <a href="../life/2.html">
            
                    
                    2.周杰伦专辑赏析--七里香
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.手动实现broadcast和dispatch方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><b></b>&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0; dispatch &#x548C; broadcast &#x65B9;&#x6CD5;</a></li><ul><li><span class="title-icon "></span><a href="#on-&#x4E0E;-emit"><b></b>$on &#x4E0E; $emit</a></li><li><span class="title-icon "></span><a href="#vuejs-1x-&#x7684;-dispatch-&#x4E0E;-broadcast"><b></b>Vue.js 1.x &#x7684; $dispatch &#x4E0E; $broadcast</a></li><li><span class="title-icon "></span><a href="#&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><b></b>&#x81EA;&#x884C;&#x5B9E;&#x73B0; dispatch &#x548C; broadcast &#x65B9;&#x6CD5;</a></li><ul><li><span class="title-icon "></span><a href="#&#x5148;&#x601D;&#x8003;"><b></b>&#x5148;&#x601D;&#x8003;</a></li></ul></ul></ul></div><a href="#&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><a name="&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6D3E;&#x53D1;&#x4E0E;&#x5E7F;&#x64AD;&#x2014;&#x2014;&#x81EA;&#x884C;&#x5B9E;&#x73B0; dispatch &#x548C; broadcast &#x65B9;&#x6CD5;</h1>
<p>&#x4E0A;&#x4E00;&#x8282;&#x7684; provide / inject API &#x4E3B;&#x8981;&#x89E3;&#x51B3;&#x4E86;&#x8DE8;&#x7EA7;&#x7EC4;&#x4EF6;&#x95F4;&#x7684;&#x901A;&#x4FE1;&#x95EE;&#x9898;&#xFF0C;&#x4E0D;&#x8FC7;&#x5B83;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x5B50;&#x7EC4;&#x4EF6;&#x83B7;&#x53D6;&#x4E0A;&#x7EA7;&#x7EC4;&#x4EF6;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x8DE8;&#x7EA7;&#x7EC4;&#x4EF6;&#x95F4;&#x5EFA;&#x7ACB;&#x4E86;&#x4E00;&#x79CD;&#x4E3B;&#x52A8;&#x63D0;&#x4F9B;&#x4E0E;&#x4F9D;&#x8D56;&#x6CE8;&#x5165;&#x7684;&#x5173;&#x7CFB;&#x3002;&#x7136;&#x540E;&#x6709;&#x4E24;&#x79CD;&#x573A;&#x666F;&#x5B83;&#x4E0D;&#x80FD;&#x5F88;&#x597D;&#x7684;&#x89E3;&#x51B3;&#xFF1A;</p>
<p>&#x7236;&#x7EC4;&#x4EF6;&#x5411;&#x5B50;&#x7EC4;&#x4EF6;&#xFF08;&#x652F;&#x6301;&#x8DE8;&#x7EA7;&#xFF09;&#x4F20;&#x9012;&#x6570;&#x636E;&#xFF1B;</p>
<p>&#x5B50;&#x7EC4;&#x4EF6;&#x5411;&#x7236;&#x7EC4;&#x4EF6;&#xFF08;&#x652F;&#x6301;&#x8DE8;&#x7EA7;&#xFF09;&#x4F20;&#x9012;&#x6570;&#x636E;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x7236;&#x5B50;&#xFF08;&#x542B;&#x8DE8;&#x7EA7;&#xFF09;&#x4F20;&#x9012;&#x6570;&#x636E;&#x7684;&#x901A;&#x4FE1;&#x65B9;&#x5F0F;&#xFF0C;Vue.js &#x5E76;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x539F;&#x751F;&#x7684; API &#x6765;&#x652F;&#x6301;&#xFF0C;&#x800C;&#x662F;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x5927;&#x578B;&#x6570;&#x636E;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x5DE5;&#x5177; Vuex&#xFF0C;&#x800C;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x5DF2;&#x7ECF;&#x4ECB;&#x7ECD;&#x8FC7; Vuex &#x7684;&#x573A;&#x666F;&#x4E0E;&#x5728;&#x72EC;&#x7ACB;&#x7EC4;&#x4EF6;&#xFF08;&#x6216;&#x5E93;&#xFF09;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x9650;&#x5236;&#x3002;&#x672C;&#x5C0F;&#x8282;&#x5219;&#x4ECB;&#x7ECD;&#x4E00;&#x79CD;&#x5728;&#x7236;&#x5B50;&#x7EC4;&#x4EF6;&#x95F4;&#x901A;&#x4FE1;&#x7684;&#x65B9;&#x6CD5; <code>dispatch</code> &#x548C; <code>broadcast</code>&#x3002;</p>
<h2 id="on-&#x4E0E;-emit"><a name="on-&#x4E0E;-emit" class="anchor-navigation-ex-anchor" href="#on-&#x4E0E;-emit"><i class="fa fa-link" aria-hidden="true"></i></a>$on &#x4E0E; $emit</h2>
<p>&#x5728;Vue 1.x&#x7684;&#x7248;&#x672C;&#x4E2D; &#x5BF9; $dispatch &#x548C; $broadcast &#x8FD9;&#x4E24;&#x4E2A;&#x5185;&#x7F6E;&#x7684;&#x65B9;&#x6CD5;&#x5F88;&#x719F;&#x6089;&#xFF0C;&#x4E0D;&#x8FC7;&#x5B83;&#x4EEC;&#x90FD;&#x5728; Vue.js 2.x &#x91CC;&#x5E9F;&#x5F03;&#x4E86;&#x3002;&#x5728;&#x6B63;&#x5F0F;&#x4ECB;&#x7ECD;&#x4E3B;&#x89D2;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x770B;&#x770B; $on &#x4E0E; $emit &#x8FD9;&#x4E24;&#x4E2A; API&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x662F;&#x672C;&#x8282;&#x5185;&#x5BB9;&#x7684;&#x57FA;&#x7840;&#x3002;</p>
<p>$emit &#x4F1A;&#x5728;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x5B9E;&#x4F8B;&#x4E0A;&#x89E6;&#x53D1;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#xFF0C;&#x5E76;&#x4F20;&#x9012;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#x7ED9;&#x76D1;&#x542C;&#x5668;&#x7684;&#x56DE;&#x8C03;&#xFF0C;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x90FD;&#x662F;&#x5728;&#x7236;&#x7EA7;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x7EC4;&#x4EF6;&#x65F6;&#xFF0C;&#x4F7F;&#x7528; @on &#x7684;&#x65B9;&#x5F0F;&#x6765;&#x76D1;&#x542C;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#x7684;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x5B50;&#x7EC4;&#x4EF6;&#x4E2D;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// child.vue&#xFF0C;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x7701;&#x7565;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">handleEmitEvent</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">,</span> <span class="token string">&apos;Hello Vue.js&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x4E2D;&#x76D1;&#x542C;&#x7531; child.vue &#x89E6;&#x53D1;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6; test&#xFF1A;</p>
<pre class="language-"><code class="lang-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>child-component</span> <span class="token attr-name">@test</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>handleEvent<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
  <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token function">handleEvent</span> <span class="token punctuation">(</span><span class="token parameter">text</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// Hello Vue.js</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x770B;&#x4F3C;&#x662F;&#x5728;&#x7236;&#x7EC4;&#x4EF6; parent.vue &#x4E2D;&#x7ED1;&#x5B9A;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6; test &#x7684;&#x5904;&#x7406;&#x53E5;&#x67C4;&#xFF0C;&#x7136;&#x800C;&#x4E8B;&#x4EF6; test &#x5E76;&#x4E0D;&#x662F;&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x4E0A;&#x89E6;&#x53D1;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x5B50;&#x7EC4;&#x4EF6; child.vue &#x91CC;&#x89E6;&#x53D1;&#x7684;&#xFF0C;&#x53EA;&#x662F;&#x901A;&#x8FC7; v-on &#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x4E2D;&#x76D1;&#x542C;&#x3002;&#x65E2;&#x7136;&#x662F;&#x5B50;&#x7EC4;&#x4EF6;&#x81EA;&#x5DF1;&#x89E6;&#x53D1;&#x7684;&#xFF0C;&#x90A3;&#x5B83;&#x81EA;&#x5DF1;&#x4E5F;&#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x5230;&#xFF0C;&#x8FD9;&#x5C31;&#x8981;&#x4F7F;&#x7528; $on &#x6765;&#x76D1;&#x542C;&#x5B9E;&#x4F8B;&#x4E0A;&#x7684;&#x4E8B;&#x4EF6;&#xFF0C;&#x6362;&#x8A00;&#x4E4B;&#xFF0C;&#x7EC4;&#x4EF6;&#x4F7F;&#x7528; $emit &#x5728;&#x81EA;&#x5DF1;&#x5B9E;&#x4F8B;&#x4E0A;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#xFF0C;&#x5E76;&#x7528; $on &#x76D1;&#x542C;&#x5B83;&#x3002;</p>
<p>&#x542C;&#x8D77;&#x6765;&#x8FD9;&#x79CD;&#x795E;&#xFF08;s&#x101;o&#xFF09;&#x64CD;&#x4F5C;&#x6709;&#x70B9;&#x591A;&#x6B64;&#x4E00;&#x4E3E;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x59A8;&#x5148;&#x6765;&#x770B;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>handleEmitEvent<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>&#x89E6;&#x53D1;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
  <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      <span class="token function">handleEmitEvent</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5728;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x4E0A;&#x89E6;&#x53D1;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6; test&#xFF0C;&#x5E76;&#x4F20;&#x503C;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">,</span> <span class="token string">&apos;Hello Vue.js&apos;</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// &#x76D1;&#x542C;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6; test</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$on</span><span class="token punctuation">(</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">text</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        window<span class="token punctuation">.</span><span class="token function">alert</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<p>$on &#x76D1;&#x542C;&#x4E86;&#x81EA;&#x5DF1;&#x89E6;&#x53D1;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6; test&#xFF0C;&#x56E0;&#x4E3A;&#x6709;&#x65F6;&#x4E0D;&#x786E;&#x5B9A;&#x4F55;&#x65F6;&#x4F1A;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E00;&#x822C;&#x4F1A;&#x5728; mounted &#x6216; created &#x94A9;&#x5B50;&#x4E2D;&#x6765;&#x76D1;&#x542C;&#x3002;</p>
<p>&#x4EC5;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x7684;&#x786E;&#x662F;&#x591A;&#x6B64;&#x4E00;&#x4E3E;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5927;&#x53EF;&#x5728; handleEmitEvent &#x91CC;&#x76F4;&#x63A5;&#x5199; window.alert(text)&#xFF0C;&#x6CA1;&#x5FC5;&#x8981;&#x7ED5;&#x4E00;&#x5708;&#x3002;</p>
<p>&#x4E4B;&#x6240;&#x4EE5;&#x591A;&#x6B64;&#x4E00;&#x4E3E;&#xFF0C;&#x662F;&#x56E0;&#x4E3A; handleEmitEvent &#x662F;&#x5F53;&#x524D;&#x7EC4;&#x4EF6;&#x5185;&#x7684; <code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span></code> &#x8C03;&#x7528;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E0D;&#x662F;&#x5B83;&#x81EA;&#x5DF1;&#x8C03;&#x7528;&#xFF0C;&#x800C;&#x662F;&#x5176;&#x5B83;&#x7EC4;&#x4EF6;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x90A3;&#x8FD9;&#x4E2A;&#x7528;&#x6CD5;&#x5C31;&#x5927;&#x6709;&#x53EF;&#x4E3A;&#x4E86;&#x3002;</p>
<p>&#x4E86;&#x89E3;&#x4E86; $on &#x548C; $emit &#x7684;&#x7528;&#x6CD5;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x518D;&#x6765;&#x770B;&#x4E24;&#x4E2A;&#x201C;&#x8FC7;&#x65F6;&#x7684;&#x201D; API&#x3002;</p>
<h2 id="vuejs-1x-&#x7684;-dispatch-&#x4E0E;-broadcast"><a name="vuejs-1x-&#x7684;-dispatch-&#x4E0E;-broadcast" class="anchor-navigation-ex-anchor" href="#vuejs-1x-&#x7684;-dispatch-&#x4E0E;-broadcast"><i class="fa fa-link" aria-hidden="true"></i></a>Vue.js 1.x &#x7684; $dispatch &#x4E0E; $broadcast</h2>
<p>&#x867D;&#x7136; Vue.js 1.x &#x5DF2;&#x7ECF;&#x6210;&#x4E3A;&#x8FC7;&#x53BB;&#x65F6;&#xFF0C;&#x4F46;&#x4E3A;&#x4E86;&#x5145;&#x5206;&#x7406;&#x89E3;&#x672C;&#x8282;&#x901A;&#x4FE1;&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF0C;&#x8FD8;&#x662F;&#x6709;&#x5FC5;&#x8981;&#x6765;&#x4E86;&#x89E3;&#x4E00;&#x70B9;&#x5B83;&#x7684;&#x5386;&#x53F2;&#x3002;</p>
<p>&#x5728; Vue.js 1.x &#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;$dispatch &#x548C; $broadcast &#xFF0C;&#x524D;&#x8005;&#x7528;&#x4E8E;&#x5411;&#x4E0A;&#x7EA7;&#x6D3E;&#x53D1;&#x4E8B;&#x4EF6;&#xFF0C;&#x53EA;&#x8981;&#x662F;&#x5B83;&#x7684;&#x7236;&#x7EA7;&#xFF08;&#x4E00;&#x7EA7;&#x6216;&#x591A;&#x7EA7;&#x4EE5;&#x4E0A;&#xFF09;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x5728;&#x7EC4;&#x4EF6;&#x5185;&#x901A;&#x8FC7; $on &#xFF08;&#x6216; events&#xFF0C;2.x &#x5DF2;&#x5E9F;&#x5F03;&#xFF09;&#x76D1;&#x542C;&#x5230;&#xFF0C;&#x540E;&#x8005;&#x76F8;&#x53CD;&#xFF0C;&#x662F;&#x7531;&#x4E0A;&#x7EA7;&#x5411;&#x4E0B;&#x7EA7;&#x5E7F;&#x64AD;&#x4E8B;&#x4EF6;&#x7684;&#x3002;</p>
<p>&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-html"><span class="token comment">&lt;!-- &#x6CE8;&#x610F;&#xFF1A;&#x8BE5;&#x793A;&#x4F8B;&#x4E3A; Vue.js 1.x &#x7248;&#x672C; --&gt;</span>
<span class="token comment">&lt;!-- &#x5B50;&#x7EC4;&#x4EF6; --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>handleDispatch<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>&#x6D3E;&#x53D1;&#x4E8B;&#x4EF6;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">handleDispatch</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$dispatch</span><span class="token punctuation">(</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">,</span> <span class="token string">&apos;Hello, Vue.js&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<pre class="language-"><code class="lang-html"><span class="token comment">&lt;!-- &#x7236;&#x7EC4;&#x4EF6;&#xFF0C;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x7701;&#x7565; --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>child-component</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>child-component</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
  <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$on</span><span class="token punctuation">(</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">text</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// Hello, Vue.js</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre>
<p>$broadcast &#x7C7B;&#x4F3C;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x65B9;&#x5411;&#x76F8;&#x53CD;&#x3002;&#x8FD9;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#x4E00;&#x65E6;&#x53D1;&#x51FA;&#x4E8B;&#x4EF6;&#x540E;&#xFF0C;&#x4EFB;&#x4F55;&#x7EC4;&#x4EF6;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x5230;&#x7684;&#xFF0C;&#x5C31;&#x8FD1;&#x539F;&#x5219;&#xFF0C;&#x800C;&#x4E14;&#x4F1A;&#x5728;&#x7B2C;&#x4E00;&#x6B21;&#x63A5;&#x6536;&#x5230;&#x540E;&#x505C;&#x6B62;&#x5192;&#x6CE1;&#xFF0C;&#x9664;&#x975E;&#x8FD4;&#x56DE; true&#x3002;</p>
<p>&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x867D;&#x7136;&#x770B;&#x8D77;&#x6765;&#x5F88;&#x597D;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x5728; Vue.js 2.x &#x4E2D;&#x90FD;&#x5E9F;&#x5F03;&#x4E86;&#xFF0C;&#x5B98;&#x65B9;&#x7ED9;&#x51FA;&#x7684;&#x89E3;&#x91CA;&#x662F;&#xFF1A;</p>
<blockquote>
<p>&#x56E0;&#x4E3A;&#x57FA;&#x4E8E;&#x7EC4;&#x4EF6;&#x6811;&#x7ED3;&#x6784;&#x7684;&#x4E8B;&#x4EF6;&#x6D41;&#x65B9;&#x5F0F;&#x6709;&#x65F6;&#x8BA9;&#x4EBA;&#x96BE;&#x4EE5;&#x7406;&#x89E3;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x7EC4;&#x4EF6;&#x7ED3;&#x6784;&#x6269;&#x5C55;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x53D8;&#x5F97;&#x8D8A;&#x6765;&#x8D8A;&#x8106;&#x5F31;&#x3002;</p>
</blockquote>
<p>&#x867D;&#x7136;&#x5728;&#x4E1A;&#x52A1;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5B83;&#x6CA1;&#x6709; Vuex &#x8FD9;&#x6837;&#x4E13;&#x95E8;&#x7BA1;&#x7406;&#x72B6;&#x6001;&#x7684;&#x63D2;&#x4EF6;&#x6E05;&#x6670;&#x597D;&#x7528;&#xFF0C;&#x4F46;&#x5BF9;&#x72EC;&#x7ACB;&#x7EC4;&#x4EF6;&#xFF08;&#x5E93;&#xFF09;&#x7684;&#x5F00;&#x53D1;&#xFF0C;&#x7EDD;&#x5BF9;&#x662F;&#x798F;&#x97F3;&#x3002;&#x56E0;&#x4E3A;&#x72EC;&#x7ACB;&#x7EC4;&#x4EF6;&#x4E00;&#x822C;&#x5C42;&#x7EA7;&#x5E76;&#x4E0D;&#x4F1A;&#x5F88;&#x590D;&#x6742;&#xFF0C;&#x5E76;&#x4E14;&#x5265;&#x79BB;&#x4E86;&#x4E1A;&#x52A1;&#xFF0C;&#x4E0D;&#x4F1A;&#x53D8;&#x7684;&#x96BE;&#x4EE5;&#x7EF4;&#x62A4;&#x3002;</p>
<p>&#x77E5;&#x9053;&#x4E86; $dispatch &#x548C; $broadcast &#x7684;&#x524D;&#x4E16;&#x4ECA;&#x751F;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C31;&#x5728; Vue.js 2.x &#x4E2D;&#x81EA;&#x884C;&#x5B9E;&#x73B0;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><a name="&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x81EA;&#x884C;&#x5B9E;&#x73B0;-dispatch-&#x548C;-broadcast-&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x81EA;&#x884C;&#x5B9E;&#x73B0; dispatch &#x548C; broadcast &#x65B9;&#x6CD5;</h2>
<h3 id="&#x5148;&#x601D;&#x8003;"><a name="&#x5148;&#x601D;&#x8003;" class="anchor-navigation-ex-anchor" href="#&#x5148;&#x601D;&#x8003;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5148;&#x601D;&#x8003;</h3>
<p>&#x6211;&#x4EEC;&#x8BE5;&#x600E;&#x4E48;&#x8C03;&#x7528;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x60F3;&#x597D;&#x4E86;&#x600E;&#x4E48;&#x4F7F;&#x7528;&#x518D;&#x53BB;&#x5B9E;&#x73B0;&#x4ED6;&#x7684;API &#x4F1A;&#x6BD4;&#x8F83;&#x9002;&#x5408;&#xFF0C;&#x5728;&#x60F3;&#x8C61;&#x4E2D;&#x8BE5;&#x65B9;&#x6CD5;&#x53EF;&#x80FD;&#x5728;&#x5F88;&#x591A;&#x7EC4;&#x4EF6;&#x4E2D;&#x90FD;&#x4F1A;&#x4F7F;&#x7528;&#xFF0C;&#x590D;&#x7528;&#x8D77;&#x89C1;&#xFF0C;&#x6211;&#x4EEC;&#x5C01;&#x88C5;&#x5728;&#x6DF7;&#x5408;&#xFF08;mixins&#xFF09;&#x91CC;&#x3002;&#x90A3;&#x5B83;&#x7684;&#x4F7F;&#x7528;&#x6837;&#x4F8B;&#x53EF;&#x80FD;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x90E8;&#x5206;&#x4EE3;&#x7801;&#x7701;&#x7565;</span>
<span class="token keyword">import</span> Emitter <span class="token keyword">from</span> <span class="token string">&apos;../mixins/emitter.js&apos;</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  mixins<span class="token punctuation">:</span> <span class="token punctuation">[</span> Emitter <span class="token punctuation">]</span><span class="token punctuation">,</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">handleDispatch</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 1</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token function">handleBroadcast</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 2</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5C31;&#x5F00;&#x59CB; &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x6765;&#x5B9E;&#x73B0;dispatch &#x548C;broadcast&#x65B9;&#x6CD5;</p>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x6838;&#x5FC3;&#x5C31;&#x662F;</p>
<p>&#x5728;&#x5B50;&#x7EC4;&#x4EF6;&#x8C03;&#x7528; dispatch &#x65B9;&#x6CD5;&#xFF0C;&#x5411;&#x4E0A;&#x7EA7;&#x6307;&#x5B9A;&#x7684;&#x7EC4;&#x4EF6;&#x5B9E;&#x4F8B;&#xFF08;&#x6700;&#x8FD1;&#x7684;&#xFF09;&#x4E0A;&#x89E6;&#x53D1;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#xFF0C;&#x5E76;&#x4F20;&#x9012;&#x6570;&#x636E;&#xFF0C;&#x4E14;&#x8BE5;&#x4E0A;&#x7EA7;&#x7EC4;&#x4EF6;&#x5DF2;&#x9884;&#x5148;&#x901A;&#x8FC7; $on &#x76D1;&#x542C;&#x4E86;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF1B;</p>
<p>&#x76F8;&#x53CD;&#xFF0C;&#x5728;&#x7236;&#x7EC4;&#x4EF6;&#x8C03;&#x7528; broadcast &#x65B9;&#x6CD5;&#xFF0C;&#x5411;&#x4E0B;&#x7EA7;&#x6307;&#x5B9A;&#x7684;&#x7EC4;&#x4EF6;&#x5B9E;&#x4F8B;&#xFF08;&#x6700;&#x8FD1;&#x7684;&#xFF09;&#x4E0A;&#x89E6;&#x53D1;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#xFF0C;&#x5E76;&#x4F20;&#x9012;&#x6570;&#x636E;&#xFF0C;&#x4E14;&#x8BE5;&#x4E0B;&#x7EA7;&#x7EC4;&#x4EF6;&#x5DF2;&#x9884;&#x5148;&#x901A;&#x8FC7; $on &#x76D1;&#x542C;&#x4E86;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#x3002;</p>
<p>&#x5B9E;&#x73B0;&#x8FD9;&#x5BF9;&#x65B9;&#x6CD5;&#x7684;&#x5173;&#x952E;&#x70B9;&#x5728;&#x4E8E;&#xFF0C;&#x5982;&#x4F55;&#x6B63;&#x786E;&#x5730;&#x5411;&#x4E0A;&#x6216;&#x5411;&#x4E0B;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;&#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x5728;&#x5B83;&#x4E0A;&#x9762;&#x89E6;&#x53D1;&#x65B9;&#x6CD5;&#x3002;&#x5728;&#x8BBE;&#x8BA1;&#x4E00;&#x4E2A;&#x65B0;&#x529F;&#x80FD;&#xFF08;features&#xFF09;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x5148;&#x786E;&#x5B9A;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x7684; API &#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x65B9;&#x6CD5;&#x540D;&#x3001;&#x53C2;&#x6570;&#x3001;&#x4F7F;&#x7528;&#x6837;&#x4F8B;&#xFF0C;&#x786E;&#x5B9A;&#x597D; API&#xFF0C;&#x518D;&#x6765;&#x5199;&#x5177;&#x4F53;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x90A3;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5E94;&#x8BE5;&#x662F;&#x4E09;&#x4E2A;&#x53C2;&#x6570; &#x7B2C;&#x4E00;&#x4E2A;&#x662F;&#x7EC4;&#x4EF6;&#x7684; name &#x503C;&#xFF0C;&#x7528;&#x4E8E;&#x5411;&#x4E0A;&#x6216;&#x5411;&#x4E0B;&#x9012;&#x5F52;&#x904D;&#x5386;&#x6765;&#x5BFB;&#x627E;&#x5BF9;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x548C;&#x7B2C;&#x4E09;&#x4E2A;&#x5C31;&#x662F;&#x4E0A;&#x6587;&#x5206;&#x6790;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#x540D;&#x79F0;&#x548C;&#x8981;&#x4F20;&#x9012;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<p>&#x4E0A;&#x83DC;&#xFF1A;</p>
<pre class="language-"><code class="lang-js">
<span class="token keyword">function</span> <span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token parameter">componentName<span class="token punctuation">,</span> eventName<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>$children<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">child</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> name <span class="token operator">=</span> child<span class="token punctuation">.</span>$options<span class="token punctuation">.</span>name<span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>name <span class="token operator">===</span> componentName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      child<span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>child<span class="token punctuation">,</span> <span class="token punctuation">[</span>eventName<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">broadcast</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>child<span class="token punctuation">,</span> <span class="token punctuation">[</span>componentName<span class="token punctuation">,</span> eventName<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span>params<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token parameter">componentName<span class="token punctuation">,</span> eventName<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">let</span> parent <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$parent <span class="token operator">||</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$root<span class="token punctuation">;</span>
      <span class="token keyword">let</span> name <span class="token operator">=</span> parent<span class="token punctuation">.</span>$options<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
      <span class="token keyword">while</span> <span class="token punctuation">(</span>parent <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token operator">!</span>name <span class="token operator">||</span> name <span class="token operator">!==</span> componentName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        parent <span class="token operator">=</span> parent<span class="token punctuation">.</span>$parent<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>parent<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          name <span class="token operator">=</span> parent<span class="token punctuation">.</span>$options<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>parent<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        parent<span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>parent<span class="token punctuation">,</span> <span class="token punctuation">[</span>eventName<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token parameter">componentName<span class="token punctuation">,</span> eventName<span class="token punctuation">,</span> params</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">broadcast</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> componentName<span class="token punctuation">,</span> eventName<span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; YoungGary 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2021-09-09 22:08:17
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.html" class="navigation navigation-prev " aria-label="Previous page: 1.provide/inject">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../js/form.html" class="navigation navigation-next " aria-label="Next page: 3.自己动手实现带校验的Form表单组件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.手动实现broadcast和dispatch方法","level":"1.4.2","depth":2,"next":{"title":"3.自己动手实现带校验的Form表单组件","level":"1.4.3","depth":2,"path":"js/form.md","ref":"js/form.md","articles":[]},"previous":{"title":"1.provide/inject","level":"1.4.1","depth":2,"path":"vue/1.md","ref":"vue/1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-comscore","prism","-highlight","copy-code-button","search-pro","-search","-lunr","expandable-chapters","splitter","-sharing","tbfed-pagefooter","anchor-navigation-ex"],"styles":{},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy YoungGary 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"],"lang":{"shell":"bash"}},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"theme-comscore":{},"copy-code-button":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"Gary","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gary的小窝","language":"zh-hans","gitbook":"*","description":"Gary的小窝. 里面包含了个人撰写的技术文章"},"file":{"path":"vue/2.md","mtime":"2021-09-09T14:08:17.742Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-09T14:10:10.930Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

